package com.ye.personnel.controller;

import com.ye.personnel.api.ChangeStatusDto;
import com.ye.personnel.api.department.DepartmentListFrListSearch;
import com.ye.personnel.api.department.DepartmentListFrVo;
import com.ye.personnel.api.department.DepartmentTreeVo;
import com.ye.personnel.api.department.SaveDepartmentNumDto;
import com.ye.personnel.api.gwy.GwyMemberListVo;
import com.ye.personnel.api.gwy.MemberListByDepartmentNoSearch;
import com.ye.personnel.api.user.AdminUserListVo;
import com.ye.personnel.api.user.AdminUserSetDto;
import com.ye.personnel.api.user.ChangeUserPasswordDto;
import com.ye.personnel.common.page.BasePageParam;
import com.ye.personnel.common.page.PageModel;
import com.ye.personnel.common.response.BaseResult;
import com.ye.personnel.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

;import java.util.List;

/**
 * @Author: yeubo
 * @Date: 2023/11/17 18:00
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户相关")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("userList")
    @ApiOperation("用户列表")
    public BaseResult<PageModel<AdminUserListVo>> userList(BasePageParam pageParam){
        PageModel<AdminUserListVo> pageModel = userService.userList(pageParam);
        return new BaseResult<PageModel<AdminUserListVo>>().ok(pageModel);
    }

    @GetMapping("userDetail")
    @ApiOperation("用户详情")
    public BaseResult<AdminUserListVo> userDetail(@RequestParam("adminUserId") String adminUserId){
        AdminUserListVo detail = userService.userDetail(adminUserId);
        return new BaseResult<AdminUserListVo>().ok(detail);
    }

    @PostMapping("setUser")
    @ApiOperation("添加用户地址")
    public BaseResult<String> setUser(@Validated @RequestBody AdminUserSetDto params){
        boolean result = userService.setUser(params);
        if(result){
            return new BaseResult<String>().success("添加用户地址成功");
        }
        return new BaseResult<String>().error("添加用户地址失败");
    }


    @PostMapping("editUser")
    @ApiOperation("编辑用户")
    public BaseResult<String> editUser(@Validated({Update.class}) @RequestBody AdminUserSetDto params){
        boolean result = userService.setUser(params);
        if(result){
            return new BaseResult<String>().success("编辑用户成功");
        }
        return new BaseResult<String>().error("编辑用户失败");
    }


    @GetMapping("deleteUser")
    @ApiOperation("删除用户")
    public BaseResult<String> deleteUser(@RequestParam("adminUserId") String adminUserId){
        boolean result = userService.deleteUser(adminUserId);
        if(result){
            return new BaseResult<String>().success("删除用户成功");
        }
        return new BaseResult<String>().error("删除用户失败");
    }

    @PostMapping("changeStatus")
    @ApiOperation("启禁用用户")
    public BaseResult<String> changeStatus(@RequestBody ChangeStatusDto statusDto){
        boolean result = userService.changeStatus(statusDto);
        if(result){
            return new BaseResult<String>().success("启禁用用户成功");
        }
        return new BaseResult<String>().error("启禁用用户失败");
    }


    @PostMapping("changeUserPassword")
    @ApiOperation("用户修改密码")
    public BaseResult<String> changeUserPassword(@Validated @RequestBody ChangeUserPasswordDto passwordDto){
        boolean result = userService.changeUserPassword(passwordDto);
        if(result){
            return new BaseResult<String>().success("密码修改成功");
        }
        return new BaseResult<String>().error("密码修改失败");
    }

    @GetMapping("resetUserPassword")
    @ApiOperation("重置用户密码")
    public BaseResult<String> resetUserPassword(@RequestParam("adminUserId") String adminUserId){
        boolean result = userService.resetUserPassword(adminUserId);
        if(result){
            return new BaseResult<String>().success("重置用户密码成功");
        }
        return new BaseResult<String>().error("重置用户密码失败");
    }
    
    @GetMapping("departmentTree")
    @ApiOperation("单位层级(只展示一级下级)")
    public BaseResult<List<DepartmentTreeVo>> departmentTree(@RequestParam(value = "parentDepartmentId",required = false) String parentDepartmentId){
        List<DepartmentTreeVo> departmentTreeVos = userService.departmentTree(parentDepartmentId);
        return new BaseResult<List<DepartmentTreeVo>>().ok(departmentTreeVos);
    }

    @PostMapping("saveDepartmentNum")
    @ApiOperation("保存部门编制人数")
    public BaseResult<String> saveDepartmentNum(@RequestBody SaveDepartmentNumDto saveDepartmentNumDto){
        boolean result = userService.saveDepartmentNum(saveDepartmentNumDto);
        if(result){
            return new BaseResult<String>().success("部门编制人数保存成功");
        }
        return new BaseResult<String>().error("部门编制人数保存失败");
    }

    @GetMapping("departmentListFr")
    @ApiOperation("法人单位")
    public BaseResult<PageModel<DepartmentListFrVo>> departmentListFr(DepartmentListFrListSearch search){
        PageModel<DepartmentListFrVo> pageModel = userService.departmentListFr(search);
        return new BaseResult<PageModel<DepartmentListFrVo>>().ok(pageModel);
    }


    @GetMapping("departmentListFrTree")
    @ApiOperation("法人单位-层级结构")
    public BaseResult<List<DepartmentListFrVo>> departmentListFrTree(DepartmentListFrListSearch search){
        List<DepartmentListFrVo> pageModel = userService.departmentListFrTree(search);
        return new BaseResult<List<DepartmentListFrVo>>().ok(pageModel);
    }

    @GetMapping("memberListByDepartmentNo")
    @ApiOperation("部门人员列表")
    public BaseResult<PageModel<GwyMemberListVo>> memberListByDepartmentNo(MemberListByDepartmentNoSearch search){
        PageModel<GwyMemberListVo> gwyMemberListVoPageModel = userService.memberListByDepartmentNo(search);
        return new BaseResult<PageModel<GwyMemberListVo>>().ok(gwyMemberListVoPageModel);
    }
}
